/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_VLOG_H__
#define __PONY_VLOG_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdio.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_config.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/

typedef enum {
    VLOG_DEBUG = 0, /**< Debug */
    VLOG_INFO,      /**< Information */
    VLOG_NOTICE,    /**< Notice */
    VLOG_WARNING,   /**< Warning */
    VLOG_ERR,       /**< Error */
    VLOG_CRIT,      /**< Critical */
    VLOG_ALERT,     /**< Alert */
    VLOG_EMERG,     /**< Emergency */
    VLOG_MAX
} vlog_t;

/* macro ---------------------------------------------------------------------*/

#ifdef CONFIG_VLOG

extern void vlog_set_level(vlog_t level);
extern vlog_t vlog_get_level(void);

extern const char *vlog_get_name(vlog_t level);

#define vlog(level, format, ...)                                                                   \
    do {                                                                                           \
        if ((level) >= vlog_get_level()) {                                                         \
            (void)printf("[%s][%s:%d] " format "\r\n", vlog_get_name((level)), __FILE__, __LINE__, \
                         ##__VA_ARGS__);                                                           \
        }                                                                                          \
    } while (0)

#define vlog_print(format, ...)                                                                    \
    printf("[%s:%d] " format "\r\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)

#else

#define vlog_set_level(level)
#define vlog_get_level() VLOG_MAX

#define vlog(level, format, ...)
#define vlog_print(format, ...)

#endif   // CONFIG_VLOG

#define vlog_debug(format, ...)   vlog(VLOG_DEBUG, format, ##__VA_ARGS__)
#define vlog_info(format, ...)    vlog(VLOG_INFO, format, ##__VA_ARGS__)
#define vlog_notice(format, ...)  vlog(VLOG_NOTICE, format, ##__VA_ARGS__)
#define vlog_warning(format, ...) vlog(VLOG_WARNING, format, ##__VA_ARGS__)
#define vlog_error(format, ...)   vlog(VLOG_ERR, format, ##__VA_ARGS__)
#define vlog_crit(format, ...)    vlog(VLOG_CRIT, format, ##__VA_ARGS__)
#define vlog_alert(format, ...)   vlog(VLOG_ALERT format, ##__VA_ARGS__)
#define vlog_tmerg(format, ...)   vlog(VLOG_EMERG, format, ##__VA_ARGS__)

#define VLOG_D vlog_debug
#define VLOG_I vlog_info
#define VLOG_W vlog_warning
#define VLOG_E vlog_error

/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_VLOG_H__ */
